﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PCLawPrototype
{
    public partial class ManageContact : ManageRibbonForm
    {
        List<Contact> _contacts = new List<Contact>();


        public ManageContact()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;

            FillToGrid();
        }

        private void FillToGrid()
        {
            _contacts = Common.Entity.Contacts.ToList();
            lstClient.Items.Clear();

            foreach (Contact contact in _contacts)
            {
                contact.Clients.Load();
                System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {
                (_contacts.IndexOf(contact) + 1).ToString("0"),
                contact.Clients.Aggregate("", (str, i) => str + i.ClientName + ", "),
                contact.ContactFirstName + ", " + contact.ContactMiddleName + " " + contact.ContactLastName,
                contact.ContactBusinessPhone,
                contact.ContactCellPhone,
                contact.ContactEmail1,
                contact.ContactPosition}, -1);
                this.lstClient.Items.Add(listViewItem1);
            }
        }

        private void ManageContact_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
            ((DStarter)this.MdiParent).SetRPEventVisible(false);
        }
        #region Overrides of ManageRibbonForm

        public override void Add()
        {
            NewContact newContact = new NewContact();
            newContact.ShowDialog();

            Reload();
        }

        public override void Edit()
        {
            NewContact newContact;
            if (lstClient.SelectedItems.Count != 0)
            {
                if (lstClient.SelectedIndices.Count != 0)
                {
                    newContact = new NewContact(_contacts[lstClient.SelectedIndices[0]]);
                    newContact.ShowDialog();

                    Reload();
                }
            }
        }

        public override void Delete()
        {
            if (lstClient.SelectedIndices.Count > 0)
            {
                Common.Entity.DeleteObject(_contacts[lstClient.SelectedIndices[0]]);
                lstClient.Items.RemoveAt(lstClient.SelectedIndices[0]);
                Common.Entity.SaveChanges();
            }
        }

        public override void Reload()
        {
            Common.Entity.Refresh(System.Data.Objects.RefreshMode.ClientWins, Common.Entity.Contacts);
            FillToGrid();
        }

        public override void Active()
        {
        }

        public override void ViewPUClient()
        {
            PUClientFrm puClientFrm = new PUClientFrm();
            puClientFrm.ShowDialog();
        }

        public override void StartSearch()
        {
            MessageBox.Show("Date filtered!");
        }

        #endregion

        private void ManageContact_Activated(object sender, EventArgs e)
        {
            ((DStarter)this.MdiParent).SetActiveVisible(false);
            ((DStarter)this.MdiParent).SetRPGContactVisible(true);
            ((DStarter)this.MdiParent).SetEventFocus();
        }

        private void ManageContact_Deactivate(object sender, EventArgs e)
        {
            ((DStarter)this.MdiParent).SetActiveVisible(true);
            ((DStarter)this.MdiParent).SetRPGContactVisible(false);
        }

        private void lstClient_DoubleClick(object sender, EventArgs e)
        {
            NewContact newContact;
            if (lstClient.SelectedItems.Count != 0)
            {
                if (lstClient.SelectedIndices.Count != 0)
                {
                    newContact = new NewContact(_contacts[lstClient.SelectedIndices[0]]);
                    newContact.ShowDialog();

                    Reload();
                }
            }
        }

        private void lstClient_Click(object sender, EventArgs e)
        {
            ((DStarter)this.MdiParent).SetEventFocus();
        }
    }
}
